package com.example.zwh.computershop.shopping_cart;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.zwh.computershop.R;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import static android.content.ContentValues.TAG;

public class ShoppCar_Fragment extends Fragment {

    private static ShopAdapter shopAdapter;
    private static List<Shop> shopList = new ArrayList<>();
    RecyclerView recyclerView;

    private TextView mTitle;
    private Button mManage;
    private Button mOK;

    private static CheckBox AllSel;
    private TextView mTotal;
    private static TextView money;
    private Button mBuy;
    private Button mDel;
    private LinearLayout mLLEmpty;

    private static double allMoney = 0.0;


    public ShoppCar_Fragment() {
        init();
        showData();
        Log.d(TAG, "ShoppCar_Fragment: " + shopList.size());
    }

    //初始化数据
    private void init() {
        shopList.clear();
        Shop adminTest = new Shop("联想办公本","联想小新Air 14(i5-8250U/8GB/256GB/MX150)", R.drawable.q, 5799, 2);
        shopList.add(adminTest);
        Shop sp[] = new Shop[10];
        for (int i = 0; i < 10; i++) {
            sp[i]= new Shop("苹果" + i, " 联想扬天V310(i5-6200U/4GB/500GB)", R.drawable.q, 3799, 1);
            shopList.add(sp[i]);
        }
    }
    
    private void showData() {
        if (shopList.size() == 0) {
            initEmptyView();           //如果数据为空,显示空的试图
            return;
        }
    }

    //显示购物车为空
    private void initEmptyView() {
        recyclerView.setVisibility(View.GONE);
        mLLEmpty.setVisibility(View.VISIBLE);
        Log.d(TAG, "initEmptyView: hello");
    }

    //点击管理按钮和完成按钮
    private void setHide(int hide) {
        if (hide == 1) {
            mManage.setVisibility(View.GONE);
            mBuy.setVisibility(View.GONE);
            money.setVisibility(View.GONE);
            mTotal.setVisibility(View.GONE);
            ShopAdapter.setIsManage(true);
            mOK.setVisibility(View.VISIBLE);
            mDel.setVisibility(View.VISIBLE);
        }else if (hide == 2){
            mOK.setVisibility(View.GONE);
            mDel.setVisibility(View.GONE);
            ShopAdapter.setIsManage(false);
            mTotal.setVisibility(View.VISIBLE);
            mManage.setVisibility(View.VISIBLE);
            mBuy.setVisibility(View.VISIBLE);
            money.setVisibility(View.VISIBLE);
        }
    }
    private static double getMoney() {
        allMoney = 0.0;
        for (Shop i : shopList) {
            if (i.getmCheckBox())
                allMoney += i.getPrice() * i.getNum();
        }
        return allMoney;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        mLLEmpty = getActivity().findViewById(R.id.ll_empty);
        mTitle = getActivity().findViewById(R.id.tv_shop_car_title);
        mTitle.setText("购物车:(" + getLength() +")");
        mManage = getActivity().findViewById(R.id.btn_shop_car_mag);
        mManage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setHide(1);
                renovate();
            }
        });
        mOK = getActivity().findViewById(R.id.btn_shop_car_ok);
        mOK.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setHide(2);
                renovate();
            }
        });
        mBuy = getActivity().findViewById(R.id.btn_shop_car_buy);
        mDel = getActivity().findViewById(R.id.btn_shop_car_del);
        mDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Iterator<Shop> iterator = shopList.iterator();
                while (iterator.hasNext()) {
                    Shop value = iterator.next();
                    if (value.getmCheckBox()) {
                        iterator.remove();
                    }
                }
                mTitle.setText("购物车:(" + getLength() +")");
                showData();
                renovate();
            }
        });
        mTotal = getActivity().findViewById(R.id.tv_shop_car_text);
        money = getActivity().findViewById(R.id.tv_shop_car_m);
        AllSel = getActivity().findViewById(R.id.cb_shop_car_allsel);
        AllSel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //点击全选
                CheckBox all = (CheckBox)v;
                if (all.isChecked())
                    ShopAdapter.allSelect(true);
                else
                    ShopAdapter.allSelect(false);
                renovate();
            }
        });
        recyclerView = getActivity().findViewById(R.id.rlv_shop_car_list);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

        shopAdapter = new ShopAdapter(getContext(), shopList);
        recyclerView.setAdapter(shopAdapter);
    }

    public void renovate() {
        setAllMoney(getMoney());
        shopAdapter.notifyDataSetChanged();
    }

    public static void myAllCheckBoxChange() {
        AllSel.setChecked(false);
    }

    public static void setAllMoney(double allMoney) {
        money.setText("￥" + allMoney);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_shopp_car, container, false );
    }

    private int getLength() {
        return shopList.size();
    }

}
